﻿using Dto;
using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class HotelBLL : IHotelBLL
    {
        public HotelBLL(IHotelDAL<HotelModel>dal,IHotelDAL<HotelTypeModel>ldal)
        {
            Dal = dal;
            Ldal = ldal;
        }

        public IHotelDAL<HotelModel> Dal { get; }
        public IHotelDAL<HotelTypeModel> Ldal { get; }

        public async Task<int> AddHotel(HotelModel hotel)
        {
            var s = Dal.GetHotelALL().Any(a => a.HotelName == hotel.HotelName);
            if (!s)
            {
                return await Dal.AddHotelAsync(hotel);
            }
            else 
            {
                return -1;
            }
            
        }

        public HotelShowDto HotelShow(string? hid, int? tid, int pageindex, int pagesize)
        {
            var a = Dal.GetHotelALL();
            var b=Ldal.GetHotelALL();
            var sql = from c in a
                      join d in b on c.Tid equals d.Tid
                      select new 
                      {
                          c.Id,
                          c.HotelId,
                          c.Img,
                          c.HotelName,
                          c.HotelBoss,
                          c.Phone,
                          c.Address,
                          c.Tid,
                          d.HotelTypeName
                      };
            if (!string.IsNullOrEmpty(hid)) 
            {
                sql = sql.Where(a => a.HotelId == hid);
            }
            if (tid > 0) 
            {
                sql = sql.Where(a => a.Tid == tid);
            }
            var s=sql.Count();
            var m=sql.OrderBy(a => a.Id).Skip((pageindex-1)*pagesize).Take(pagesize).ToList();
            return new HotelShowDto { Count = s,Members=m };
        }
    }
}
